import { WereWolfPlayerTeleport } from "@/werewolf/components/playerteleporter";
import { WereWolfEndState } from "@/werewolf/endState/state";
import { WereWolfModule } from "@/werewolf/module";
import { WereWolfBeforeNightPhase } from "@/werewolf/phases/beforeNight";
import { WereWolfCheckEndPhase } from "@/werewolf/phases/checkEnd";
import { WereWolfDiscussPhase } from "@/werewolf/phases/discuss";
import { WereWolfFirstDayPhase } from "@/werewolf/phases/firstDay";
import { WereWolfGapPhase } from "@/werewolf/phases/gap";
import { WereWolfHunterPhase } from "@/werewolf/phases/hunter";
import { WereWolfMorningPhase } from "@/werewolf/phases/morning";
import { WereWolfOutPhase } from "@/werewolf/phases/out";
import { WereWolfSeerPhase } from "@/werewolf/phases/seer";
import { WereWolfVotePhase } from "@/werewolf/phases/vote";
import { WereWolfWitchPhase } from "@/werewolf/phases/witch";
import { WereWolfWolfPhase } from "@/werewolf/phases/wolf";
import { WereWolfPlayer, WereWolfStatus } from "@/werewolf/player";
import { AutoStopComponent } from "@sapi-game/gameComponent/common/autoStop";
import { PlayerGroupSet } from "@sapi-game/gamePlayer/groupSet";
import { Game, TeamScoreBoard } from "@sapi-game/main";
import { BuildWereWolfIdOptions, BuildWereWolfPlayerName } from "../func";
import { WereWolfDaemon } from "./daemon";

export class WereWolfMainState extends WereWolfModule.State {
    private readonly phases: (() => void)[] = [
        () => this.pushState(WereWolfBeforeNightPhase),
        () => this.pushState(WereWolfSeerPhase),
        () => this.pushState(WereWolfWolfPhase),
        () => this.pushState(WereWolfGapPhase, { gapTime: 20 }),
        () => this.pushState(WereWolfWitchPhase),
        () =>
            this.pushState(WereWolfGapPhase, {
                gapTime: 7,
                components: this.context.views.beforeMorning,
            }),
        () => this.pushState(WereWolfMorningPhase),
        () => this.pushState(WereWolfCheckEndPhase),
        () => this.pushState(WereWolfHunterPhase),
        () => this.pushState(WereWolfCheckEndPhase),
        () => this.pushState(WereWolfDiscussPhase),
        () => this.pushState(WereWolfVotePhase),
        () => this.pushState(WereWolfOutPhase),
        () => this.pushState(WereWolfHunterPhase),
        () => this.pushState(WereWolfCheckEndPhase),
    ];
    private idx = 0;

    override onEnter(): void {
        this.addComponent(AutoStopComponent<WereWolfPlayer>, {
            groupSet: new PlayerGroupSet([this.context.players]),
            onLeave: (p) => this.onPlayerLeave(p),
            onStopGame: () => this.engine.stopGame(),
        })
            .addComponent(WereWolfPlayerTeleport)
            .addComponent(WereWolfDaemon);
        //右侧计分板
        this.addComponent(
            TeamScoreBoard<WereWolfPlayer>,
            BuildWereWolfIdOptions(this.context.players)
        );
        this.getComponent(TeamScoreBoard).show();
        //添加地图自定义views
        this.addComponents(this.context.map.mainView);

        //添加首日的
        this.pushState(WereWolfFirstDayPhase);
        //自动控制state
        this.subscribe(Game.events.interval, () => {
            if (this.nextState == undefined && this.engine.isActive) {
                if (this.context.winner !== null) {
                    this.transitionTo(WereWolfEndState);
                    return;
                }
                const pushPhase = this.phases[this.idx];
                this.idx = this.idx + 1;
                if (this.idx > this.phases.length - 1) {
                    this.idx = 0;
                }
                pushPhase();
            }
        });
    }

    onPlayerLeave(p: WereWolfPlayer) {
        this.context.players.sendMessage(
            `${BuildWereWolfPlayerName(p)} §r离开了狼人杀游戏`
        );
        p.status = WereWolfStatus.Dead;
    }
}
